﻿#region Copyright Info

// 
// Majesty of Omega - The next evolution of macro management space strategy
// 
// Copyright 2007-2009 André Claaßen
// 
// This file is protected by the terms and conditions of the
// Microsoft Community License (Ms-CL 1.1), a copy of which should
// have been distributed along with this software. If not,
// you may find the license information at the following URL:
// 
// http://www.microsoft.com/resources/sharedsource/licensingbasics/communitylicense.mspx

#endregion

#region

using System;
using System.Collections.Generic;
using System.Linq;
using Majesty_of_Omega.Config;

#endregion

namespace Majesty_of_Omega.Model.Research
{
    ///<summary>
    /// The research state of a technology school
    ///</summary>
    [Serializable]
    public class TechSchoolResearch : ResearchItem
    {
        /// <summary>
        /// The corresponding technology school
        /// </summary>
        [NonSerialized] private TechSchool techSchool;

        /// <summary>
        /// List of Technologiy Fields for this School
        /// </summary>
        readonly List<TechFieldResearch> techFieldResearchForSchool = new List<TechFieldResearch>();

        /// <summary>
        /// Gets the tech field research for school.
        /// </summary>
        /// <value>The tech field research for school.</value>
        public List<TechFieldResearch> TechFieldResearchForSchool
        {
            set
            {
                throw new NotImplementedException();
            }
            get
            {
                return techFieldResearchForSchool;
            }
        }

        /// <summary>
        /// Gets only the researched tech field Research for the technology school
        /// </summary>
        /// <value>The tech field research for school.</value>
        public IEnumerable<TechFieldResearch> ResearchedTechFields
        {
            get
            {
                return from tfr in techFieldResearchForSchool
                       where tfr.Researched
                       select tfr;
            }
        }

        /// <summary>
        /// Gets the researched tech fields.
        /// </summary>
        /// <returns></returns>
        public IEnumerable<TechFieldResearch> GetResearchedTechFields()
        {
            return from techFieldResearch in techFieldResearchForSchool
                   where techFieldResearch.Researched
                   select techFieldResearch;
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="TechSchoolResearch"/> class.
        /// </summary>
        /// <param name="school">The school.</param>
        public TechSchoolResearch(TechSchool school)
            : base(school.Type_ID)
        {
            this.techSchool = school;
        }

        /// <summary>
        /// Gets the tech school.
        /// </summary>
        /// <value>The tech school.</value>
        public TechSchool TechSchool
        {
            get
            {
                if (techSchool == null)
                {
                    techSchool = SpreadSheetManager.TechTables.TechSchoolDictionary[ResearchItemTypeID];
                }
                return techSchool;
            }
        }

        /// <summary>
        /// Equalses the specified obj.
        /// </summary>
        /// <param name="obj">The obj.</param>
        /// <returns></returns>
        public bool Equals(TechSchoolResearch obj)
        {
            if (ReferenceEquals(null, obj)) return false;
            if (ReferenceEquals(this, obj)) return true;
            return base.Equals(obj);
        }

        /// <summary>
        /// Calculates the progress in current level in percent.
        /// </summary>
        /// <value>The progress in current level.</value>
        /// <remarks>
        /// THis value is not supported for technology schols
        /// </remarks>
        public override int ProgressInCurrentLevel
        {
            get { throw new NotImplementedException(); }
        }

        /// <summary>
        /// Gets the icon of this research item.
        /// </summary>
        /// <remarks>
        /// Currently, there are no icons for tech schools
        /// </remarks>
        /// <value>The icon.</value>
        public override string Icon
        {
            get { throw new NotImplementedException(); }
        }

        /// <summary>
        /// Gets the average tech level of all researched tech fields
        /// </summary>
        /// <value>The tech level.</value>
        public override int TechLevel
        {
            get
            {
                return Convert.ToInt32(GetResearchedTechFields().Average(tf => tf.TechLevel));
            }
        }

        /// <summary>
        /// Determines whether the specified <see cref="T:System.Object"/> is equal to the current <see cref="T:System.Object"/>.
        /// </summary>
        /// <param name="obj">The <see cref="T:System.Object"/> to compare with the current <see cref="T:System.Object"/>.</param>
        /// <returns>
        /// true if the specified <see cref="T:System.Object"/> is equal to the current <see cref="T:System.Object"/>; otherwise, false.
        /// </returns>
        /// <exception cref="T:System.NullReferenceException">
        /// The <paramref name="obj"/> parameter is null.
        /// </exception>
        public override bool Equals(object obj)
        {
            if (ReferenceEquals(null, obj)) return false;
            if (ReferenceEquals(this, obj)) return true;
            return Equals(obj as TechSchoolResearch);
        }

        /// <summary>
        /// Serves as a hash function for a particular type.
        /// </summary>
        /// <returns>
        /// A hash code for the current <see cref="T:System.Object"/>.
        /// </returns>
        public override int GetHashCode()
        {
            unchecked
            {
                {
                    return base.GetHashCode()*397;
                }
            }
        }
    }
}